package org.aristotle.generator.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameUtils {

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /*首字母大写*/
    public static String upperFirstLetter(String word) {
        return word.substring(0, 1).toUpperCase() + word.substring(1);
    }

    /*首字母小写*/
    public static String lowerFirstLetter(String word) {
        return word.substring(0, 1).toLowerCase() + word.substring(1);
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    /*驼峰变下划线(全小写)*/
    public static String humpToLine(String str) {
        str = lowerFirstLetter(str);
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString().toLowerCase();
    }

    /*下划线变驼峰*/
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return upperFirstLetter(sb.toString());
    }

    /*包名点转斜杠*/
    public static String dotToSlash(String str) {
        return str.replaceAll("\\.", "/");
    }


    private static Pattern transverseLinePattern = Pattern.compile("-(\\w)");

    /*横线转驼峰*/
    public static String transverseLineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = transverseLinePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return upperFirstLetter(sb.toString());
    }

}
